<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
error_reporting(0);
$mailSent = false;

if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['text']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
    $senderName = trim(filter_var($_POST['name'], FILTER_SANITIZE_STRING));
    $senderMsg = trim(filter_var($_POST['text'], FILTER_SANITIZE_STRING));
    $senderEmail = $_POST['email'];
  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
        echo json_encode($mailSent);
        exit();
    }
  }
  
  //send email
  $sendMail = mail( "contact@tealriver.com", "TealRiver Contact", "From: " . $senderName . "\r\n\r\n" . $senderMsg, "From:" . $senderEmail );
  if($sendMail) {
      $mailSent = true;
  }
  unset($_POST);
}
echo json_encode($mailSent);
exit();
?>